﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Arthus
{
    public partial class partnerForma : Form
    {
        private String idPartnera;
        public partnerForma(String partnerID)
        {
            InitializeComponent();
            this.idPartnera = partnerID;
            comboBoxPoreklo.SelectedIndex = 0;
            comboBoxVrsta.SelectedIndex = 0;
            ProveriPostojanostIdPartnera();
        }
        Partneri partneriObjekat = null;
        racunForma racunFormaObjekat = null;
        Region regionObjekat = null;

        private void ProveriPostojanostIdPartnera() {
            if (idPartnera.Length > 0)
            {
                buttonDodajPartnera.Enabled = false;
                buttonIzmeniPartnera.Enabled = true;
                
                //racuni i kontakti
                buttonDodajKontakt.Enabled = true;
                buttonDodajRacun.Enabled = true;

                if (dataGridViewRacuniPartnera.RowCount > 0)
                {
                    buttonIzmeniRacun.Enabled = true;
                    buttonObrisiRacun.Enabled = true;
                }
                else {
                    buttonIzmeniRacun.Enabled = false;
                    buttonObrisiRacun.Enabled = false;
                }

                if (dataGridViewKontaktiPartnera.RowCount > 0)
                {
                    buttonIzmeniKontakt.Enabled = true;
                    buttonObrisiKontakt.Enabled = true;
                }
                else {
                    buttonIzmeniKontakt.Enabled = false;
                    buttonObrisiKontakt.Enabled = false;
                }
            }
            else {
                buttonDodajPartnera.Enabled = true;
                buttonIzmeniPartnera.Enabled = false;

                buttonDodajKontakt.Enabled = false;
                buttonDodajRacun.Enabled = false;
                buttonIzmeniRacun.Enabled = false;
                buttonObrisiRacun.Enabled = false;
                buttonIzmeniKontakt.Enabled = false;
                buttonObrisiKontakt.Enabled = false;
            }
        }

        private void dataGridViewKontaktiPartnera_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            kontaktForma kontaktFormaObjekat = new kontaktForma(idPartnera,dataGridViewKontaktiPartnera.SelectedRows[0].Cells[0].Value.ToString());
            kontaktFormaObjekat.ShowDialog();
        }
        
        private void buttonDodajKontakt_Click(object sender, EventArgs e)
        {
            kontaktForma kontaktFormaObjekat = new kontaktForma(idPartnera, "");
            kontaktFormaObjekat.ShowDialog();
        }

        private void buttonIzmeniKontakt_Click(object sender, EventArgs e)
        {
            kontaktForma kontaktFormaObjekat = new kontaktForma(idPartnera, dataGridViewKontaktiPartnera.SelectedRows[0].Cells[0].Value.ToString());
            kontaktFormaObjekat.ShowDialog();
        }

        private void buttonObrisiKontakt_Click(object sender, EventArgs e)
        {
            partneriObjekat = new Partneri();
            partneriObjekat.ObrisiKontaktPremaID(dataGridViewKontaktiPartnera.SelectedRows[0].Cells[0].Value.ToString());
        }

        private void partnerForma_Activated(object sender, EventArgs e)
        {
            partneriObjekat = new Partneri();
            partneriObjekat.PopuniDataGridViewKontakti(dataGridViewKontaktiPartnera, idPartnera);
            partneriObjekat.PopuniDataGridViewRacuni(dataGridViewRacuniPartnera, idPartnera);
            label5.Text = idPartnera;
            ProveriPostojanostIdPartnera();
        }

        private void partnerForma_Load(object sender, EventArgs e)
        {
            partneriObjekat = new Partneri();
            regionObjekat = new Region();
            regionObjekat.PopuniComboBox(comboBoxRegion);
            partneriObjekat.PopuniKontroleUPartnerFormi(idPartnera, textBoxOznaka, textBoxNaziv, textBoxNaziv2, textBoxAdresa, textBoxAdresa2, textBoxPosta, textBoxMesto,
                textBoxTelefon, textBoxFax, textBoxMaticniBroj, textBoxPIBJMBGRGB, textBoxPDVbroj, textBoxSifraDelatnosti, checkBoxObveznikPDV, textBoxWWW, textBoxEmail,
                comboBoxRegion, comboBoxPoreklo, comboBoxVrsta, textBoxNapomena);
            partneriObjekat.PopuniDataGridViewKontakti(dataGridViewKontaktiPartnera, idPartnera);
            partneriObjekat.PopuniDataGridViewRacuni(dataGridViewRacuniPartnera, idPartnera);
            ProveriPostojanostIdPartnera();
        }

        private void dataGridViewRacuniPartnera_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            racunFormaObjekat=new racunForma(idPartnera, dataGridViewRacuniPartnera.SelectedRows[0].Cells[0].Value.ToString());
            racunFormaObjekat.ShowDialog();
        }

        private void buttonDodajRacun_Click(object sender, EventArgs e)
        {
            racunFormaObjekat = new racunForma(idPartnera, "");
            racunFormaObjekat.ShowDialog();
        }

        private void buttonIzmeniRacun_Click(object sender, EventArgs e)
        {
            racunFormaObjekat = new racunForma(idPartnera, dataGridViewRacuniPartnera.SelectedRows[0].Cells[0].Value.ToString());
            racunFormaObjekat.ShowDialog();
        }

        private void buttonObrisiRacun_Click(object sender, EventArgs e)
        {
            partneriObjekat = new Partneri();
            partneriObjekat.ObrisiRacunPremaID(dataGridViewRacuniPartnera.SelectedRows[0].Cells[0].Value.ToString());
        }

        private void buttonZatvori_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonDodajPartnera_Click(object sender, EventArgs e)
        {
            partneriObjekat = new Partneri();
            partneriObjekat.DodajIzmeniPartnera(idPartnera, textBoxOznaka, textBoxNaziv, textBoxNaziv2, textBoxAdresa, textBoxAdresa2, textBoxPosta, textBoxMesto, textBoxTelefon,
                textBoxFax, textBoxMaticniBroj, textBoxPIBJMBGRGB, textBoxPDVbroj, textBoxSifraDelatnosti, checkBoxObveznikPDV, textBoxWWW, textBoxEmail,
                comboBoxRegion, comboBoxPoreklo, comboBoxVrsta, textBoxNapomena);
            idPartnera = Convert.ToString(partneriObjekat.VratiPovecanID() - 1);
            MessageBox.Show("Uspešno dodat novi partner!");
        }

        private void VratiSveVrednostiNaPrazno() {
            textBoxOznaka.Text = "";
            textBoxNaziv.Text = "";
            textBoxNaziv2.Text = "";
            textBoxAdresa.Text = "";
            textBoxAdresa2.Text = "";
            textBoxPosta.Text = "";
            textBoxMesto.Text = "";
            textBoxTelefon.Text = "";
            textBoxFax.Text = "";
            textBoxMaticniBroj.Text = "";
            textBoxPIBJMBGRGB.Text = "";
            textBoxPDVbroj.Text = "";
            textBoxSifraDelatnosti.Text = "";
            checkBoxObveznikPDV.Checked = true;
            textBoxWWW.Text = "";
            textBoxEmail.Text = "";
            comboBoxRegion.SelectedIndex = 0;
            comboBoxPoreklo.SelectedIndex = 0;
            comboBoxVrsta.SelectedIndex = 0;
            textBoxNapomena.Text = "";
        }

        private void TABKaoENTER(KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                e.SuppressKeyPress = true;
                SelectNextControl(ActiveControl, true, true, true, true);
            }
        }
        private void textBoxOznaka_KeyDown(object sender, KeyEventArgs e)
        {
            TABKaoENTER(e);
        }
        private void textBoxNaziv_KeyDown(object sender, KeyEventArgs e)
        {
            TABKaoENTER(e);
        }
        private void textBoxNaziv2_KeyDown(object sender, KeyEventArgs e)
        {
            TABKaoENTER(e);
        }
        private void textBoxAdresa_KeyDown(object sender, KeyEventArgs e)
        {
            TABKaoENTER(e);
        }
        private void textBoxAdresa2_KeyDown(object sender, KeyEventArgs e)
        {
            TABKaoENTER(e);
        }
        private void textBoxPosta_KeyDown(object sender, KeyEventArgs e)
        {
            TABKaoENTER(e);
        }
        private void textBoxMesto_KeyDown(object sender, KeyEventArgs e)
        {
            TABKaoENTER(e);
        }
        private void textBoxTelefon_KeyDown(object sender, KeyEventArgs e)
        {
            TABKaoENTER(e);
        }
        private void textBoxFax_KeyDown(object sender, KeyEventArgs e)
        {
            TABKaoENTER(e);
        }
        private void textBoxMaticniBroj_KeyDown(object sender, KeyEventArgs e)
        {
            TABKaoENTER(e);
        }
        private void textBoxPIBJMBGRGB_KeyDown(object sender, KeyEventArgs e)
        {
            TABKaoENTER(e);
        }
        private void textBoxPDVbroj_KeyDown(object sender, KeyEventArgs e)
        {
            TABKaoENTER(e);
        }
        private void textBoxSifraDelatnosti_KeyDown(object sender, KeyEventArgs e)
        {
            TABKaoENTER(e);
        }
        private void checkBoxObveznikPDV_KeyDown(object sender, KeyEventArgs e)
        {
            TABKaoENTER(e);
        }
        private void textBoxWWW_KeyDown(object sender, KeyEventArgs e)
        {
            TABKaoENTER(e);
        }
        private void textBoxEmail_KeyDown(object sender, KeyEventArgs e)
        {
            TABKaoENTER(e);
        }
        private void comboBoxRegion_KeyDown(object sender, KeyEventArgs e)
        {
            TABKaoENTER(e);
        }
        private void comboBoxPoreklo_KeyDown(object sender, KeyEventArgs e)
        {
            TABKaoENTER(e);
        }
        private void comboBoxVrsta_KeyDown(object sender, KeyEventArgs e)
        {
            TABKaoENTER(e);
        }
        private void textBoxNapomena_KeyDown(object sender, KeyEventArgs e)
        {
            TABKaoENTER(e);
        }

        private void buttonIzmeniPartnera_Click(object sender, EventArgs e)
        {
            partneriObjekat = new Partneri();
            partneriObjekat.DodajIzmeniPartnera(idPartnera, textBoxOznaka, textBoxNaziv, textBoxNaziv2, textBoxAdresa, textBoxAdresa2, textBoxPosta, textBoxMesto, textBoxTelefon,
                textBoxFax, textBoxMaticniBroj, textBoxPIBJMBGRGB, textBoxPDVbroj, textBoxSifraDelatnosti, checkBoxObveznikPDV, textBoxWWW, textBoxEmail,
                comboBoxRegion, comboBoxPoreklo, comboBoxVrsta, textBoxNapomena);
            MessageBox.Show("Uspešna izmena!");
        }
    }
}
